/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import pojo.Nhanvien;
import dao.NhanVienDAO;
import java.util.Calendar;
import java.util.Date;
import javax.swing.JOptionPane;

/**
 *
 * @author sangnguyen
 */
public class JInternalFrameThayDoiThongTin extends javax.swing.JInternalFrame {

    /**
     * Creates new form JInternalFrameThayDoiThongTin
     */
    private Nhanvien objNhanvien;
    public JInternalFrameThayDoiThongTin() {
        initComponents();
        DateFormat formatDatetoString = new SimpleDateFormat("dd-MM-yyyy");
        objNhanvien = JFrameDangNhap.getNhanvien();
        this.jTextFieldHoten.setText(objNhanvien.getHoten());
        this.jTextAreaDiaChi.setText(objNhanvien.getDiachi());
        this.jFormattedTextFieldDienThoai.setText(objNhanvien.getDienthoai());
        this.jLabelUsername.setText(objNhanvien.getUsername());
        this.jPasswordFieldPassHienTai.setText(objNhanvien.getPassword());
        if(objNhanvien.getGioitinh()==1)
            this.jRadioButtonNam.setSelected(true);
        else
            this.jRadioButtonNu.setSelected(true);
        this.jFormattedTextFieldNgaysinh.setText(formatDatetoString.format(objNhanvien.getNgaysinh()));         
    }
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldHoten = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jFormattedTextFieldNgaysinh = new javax.swing.JFormattedTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jRadioButtonNam = new javax.swing.JRadioButton();
        jRadioButtonNu = new javax.swing.JRadioButton();
        jLabel6 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextAreaDiaChi = new javax.swing.JTextArea();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabelUsername = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jPasswordFieldPassHienTai = new javax.swing.JPasswordField();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jPasswordFieldPassMoi2 = new javax.swing.JPasswordField();
        jPasswordFieldPassMoi = new javax.swing.JPasswordField();
        jButtonThoat = new javax.swing.JButton();
        jButtonCapnhat = new javax.swing.JButton();
        jFormattedTextFieldDienThoai = new javax.swing.JFormattedTextField();

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel1.setText("Thay Đổi Thông Tin");

        jLabel2.setText("Họ tên");

        jLabel3.setText("Ngày sinh");
        jLabel3.setToolTipText("");

        jFormattedTextFieldNgaysinh.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.DateFormatter(new java.text.SimpleDateFormat("dd-MM-yyyy"))));

        jLabel4.setText("(dd-MM-yyyy)");

        jLabel5.setText("Giới tính");

        jRadioButtonNam.setText("Nam");
        jRadioButtonNam.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonNamActionPerformed(evt);
            }
        });

        jRadioButtonNu.setText("Nữ");
        jRadioButtonNu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButtonNuActionPerformed(evt);
            }
        });

        jLabel6.setText("Địa chỉ");

        jTextAreaDiaChi.setColumns(10);
        jTextAreaDiaChi.setRows(2);
        jScrollPane1.setViewportView(jTextAreaDiaChi);

        jLabel7.setText("Điện thoại");

        jLabel8.setText("Username");

        jLabelUsername.setText("Usename");

        jLabel9.setText("Password cũ");

        jPasswordFieldPassHienTai.setEditable(false);

        jLabel10.setText("Password mới");

        jLabel11.setText("Password mới lần 2");

        jButtonThoat.setText("Thoát");
        jButtonThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonThoatActionPerformed(evt);
            }
        });

        jButtonCapnhat.setText("Cập nhật");
        jButtonCapnhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonCapnhatActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(jLabel2)
                                    .addGap(38, 38, 38))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(jLabel3)
                                    .addGap(23, 23, 23)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel8)
                                    .addComponent(jLabel10))
                                .addGap(36, 36, 36)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabelUsername)
                                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 217, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jFormattedTextFieldNgaysinh, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel4))
                                    .addComponent(jPasswordFieldPassMoi, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(19, 19, 19)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel9)
                                            .addComponent(jLabel11)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel5))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel7)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jPasswordFieldPassHienTai)
                                    .addComponent(jPasswordFieldPassMoi2)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jRadioButtonNam)
                                        .addGap(23, 23, 23)
                                        .addComponent(jRadioButtonNu))
                                    .addComponent(jFormattedTextFieldDienThoai, javax.swing.GroupLayout.DEFAULT_SIZE, 129, Short.MAX_VALUE))
                                .addGap(1, 1, 1))
                            .addComponent(jTextFieldHoten)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jButtonCapnhat, javax.swing.GroupLayout.PREFERRED_SIZE, 89, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(34, 34, 34)
                                .addComponent(jButtonThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(234, 234, 234)
                        .addComponent(jLabel1)))
                .addContainerGap(34, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldHoten, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(23, 23, 23)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jFormattedTextFieldNgaysinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(jLabel5)
                    .addComponent(jRadioButtonNam)
                    .addComponent(jRadioButtonNu))
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel6)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel7)
                        .addComponent(jFormattedTextFieldDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(19, 19, 19)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jLabelUsername)
                    .addComponent(jLabel9)
                    .addComponent(jPasswordFieldPassHienTai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel10)
                    .addComponent(jLabel11)
                    .addComponent(jPasswordFieldPassMoi2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPasswordFieldPassMoi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonThoat)
                    .addComponent(jButtonCapnhat))
                .addContainerGap(43, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonThoatActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jButtonThoatActionPerformed

    private void jRadioButtonNamActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonNamActionPerformed
        // TODO add your handling code here:
        this.jRadioButtonNu.setSelected(false);
    }//GEN-LAST:event_jRadioButtonNamActionPerformed

    private void jRadioButtonNuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButtonNuActionPerformed
        // TODO add your handling code here:
        this.jRadioButtonNam.setSelected(false);
    }//GEN-LAST:event_jRadioButtonNuActionPerformed

    private void jButtonCapnhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonCapnhatActionPerformed
        try {
            // TODO add your handling code here:
            DateFormat formatDatetoString = new SimpleDateFormat("dd-MM-yyyy");
            //Nhanvien objNhanvien = new Nhanvien();
            
            objNhanvien.setHoten(this.jTextFieldHoten.getText());
            
            Date ngaySinh = formatDatetoString.parse(this.jFormattedTextFieldNgaysinh.getText());
            String strCurDay = formatDatetoString.format(Calendar.getInstance().getTime());                    
            Date currentDate = (Date)formatDatetoString.parse(strCurDay);                    
            int nYear = currentDate.getYear() - ngaySinh.getYear();
            if(nYear<18){
                JOptionPane.showMessageDialog(this, "Ngày sinh không hợp lệ (chưa đủ 18 tuổi)");
                return;
            }
            objNhanvien.setNgaysinh(ngaySinh);
            objNhanvien.setDiachi(this.jTextAreaDiaChi.getText());
            objNhanvien.setDienthoai(this.jFormattedTextFieldDienThoai.getText());
            if(this.jRadioButtonNam.isSelected()==true)
                objNhanvien.setGioitinh(1);
            else
                objNhanvien.setGioitinh(0);
            if(this.jPasswordFieldPassMoi.getText().isEmpty()==false){
                
                if(this.jPasswordFieldPassMoi.getText().compareTo(this.jPasswordFieldPassMoi2.getText())==0 && this.jPasswordFieldPassMoi.getText().length()>6)            
                    objNhanvien.setPassword(this.jPasswordFieldPassMoi.getText().trim());         
                else{
                    JOptionPane.showMessageDialog(this, "Password mới không hợp lệ!");    
                    return;
                }
            }
            if(NhanVienDAO.UpdateNhanvien(objNhanvien)){
                JOptionPane.showMessageDialog(this, "Cập nhật thành công!");                
            }
            else{
                JOptionPane.showMessageDialog(this, "Cập nhật thất bại!");                
                return;
            }
            
        } catch (ParseException ex) {
            Logger.getLogger(JInternalFrameThayDoiThongTin.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }//GEN-LAST:event_jButtonCapnhatActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonCapnhat;
    private javax.swing.JButton jButtonThoat;
    private javax.swing.JFormattedTextField jFormattedTextFieldDienThoai;
    private javax.swing.JFormattedTextField jFormattedTextFieldNgaysinh;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JLabel jLabelUsername;
    private javax.swing.JPasswordField jPasswordFieldPassHienTai;
    private javax.swing.JPasswordField jPasswordFieldPassMoi;
    private javax.swing.JPasswordField jPasswordFieldPassMoi2;
    private javax.swing.JRadioButton jRadioButtonNam;
    private javax.swing.JRadioButton jRadioButtonNu;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea jTextAreaDiaChi;
    private javax.swing.JTextField jTextFieldHoten;
    // End of variables declaration//GEN-END:variables
}
